package org.dromara.book.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.book.domain.vo.SzlyBaseBookVersionVo;
import org.dromara.book.domain.bo.SzlyBaseBookVersionBo;
import org.dromara.book.service.ISzlyBaseBookVersionService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 版本号
 * 前端访问路由地址为:/book/baseBookVersion
 *
 * @author LionLi
 * @date 2024-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseBookVersion")
public class SzlyBaseBookVersionController extends BaseController {

    private final ISzlyBaseBookVersionService szlyBaseBookVersionService;

    /**
     * 查询版本号列表
     */
    @SaCheckPermission("book:baseBookVersion:list")
    @GetMapping("/list")
    public TableDataInfo<SzlyBaseBookVersionVo> list(SzlyBaseBookVersionBo bo, PageQuery pageQuery) {
        return szlyBaseBookVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 版次下拉选择
     */
    @GetMapping("/list2Select")
    public R<List<SzlyBaseBookVersionVo>> list2Select() {
        return R.ok(szlyBaseBookVersionService.queryList(new SzlyBaseBookVersionBo()));
    }

    /**
     * 导出版本号列表
     */
    @SaCheckPermission("book:baseBookVersion:export")
    @Log(title = "版本号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SzlyBaseBookVersionBo bo, HttpServletResponse response) {
        List<SzlyBaseBookVersionVo> list = szlyBaseBookVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "版本号", SzlyBaseBookVersionVo.class, response);
    }

    /**
     * 获取版本号详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("book:baseBookVersion:query")
    @GetMapping("/{id}")
    public R<SzlyBaseBookVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(szlyBaseBookVersionService.queryById(id));
    }

    /**
     * 新增版本号
     */
    @SaCheckPermission("book:baseBookVersion:add")
    @Log(title = "版本号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SzlyBaseBookVersionBo bo) {
        return toAjax(szlyBaseBookVersionService.insertByBo(bo));
    }

    /**
     * 修改版本号
     */
    @SaCheckPermission("book:baseBookVersion:edit")
    @Log(title = "版本号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SzlyBaseBookVersionBo bo) {
        return toAjax(szlyBaseBookVersionService.updateByBo(bo));
    }

    /**
     * 删除版本号
     *
     * @param ids 主键串
     */
    @SaCheckPermission("book:baseBookVersion:remove")
    @Log(title = "版本号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(szlyBaseBookVersionService.deleteWithValidByIds(List.of(ids), true));
    }
}
